// @algorithm @lc id=43 lang=cpp 
// @title multiply-strings


// #define print(...)
// @test("2","3")="6"
// @test("123","456")="56088"
// @test("0","0")="0"
class Solution {
public:
    string multiply(string num1, string num2) {
        int m = num1.size(), n = num2.size();
        if(1==m && num1[0]=='0') return "0";
        if(1==n && num2[0]=='0') return "0";
        vector<int> res(m+n, 0);
        auto it = res.rbegin();
        for(auto ni=num2.rbegin(); ni!=num2.rend(); ni++,it++){
            auto ri = it;
            for(auto mi=num1.rbegin(); mi!=num1.rend(); mi++,ri++){
                int product = (*mi-'0') * (*ni-'0');
                *ri += product;
                if(*ri>9){
                    *(ri+1) += *ri/10;
                    *ri = *ri%10;
                }
            }
        }
        string ans;
        auto p = res.begin();
        while(!*p && p!=res.end()) p++;
        while(p!=res.end()) ans.push_back('0'+*p++);
        return ans;
    }
};